package api_sys

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"wrblog-api-go/app/common/token"
	"wrblog-api-go/app/model/model_sys"
	"wrblog-api-go/app/service/service_sys"
	"wrblog-api-go/pkg/request"
	"wrblog-api-go/pkg/result"
	"wrblog-api-go/pkg/status"
)

// @Tags  System - 字典管理
// @Summary  获取字典数据列表
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param sysDictData query model_sys.SelectSysDictData true "sysDictData"
// @Success 200 {object} result.Result "OK"
// @Router /sysDict/data/page [get]
func ApiDictDataPageList(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	sysDictData := raw.(model_sys.SelectSysDictData)
	result.PageRes(service_sys.GetDictDataPageList(sysDictData)).Json(c)
}

// @Tags  System - 字典管理
// @Summary  获取指定字典数据列表
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param dictType path string true "dictType"
// @Success 200 {object} result.Result "OK"
// @Router /sysDict/data/list/{dictType} [get]
func ApiDictDataByType(c *gin.Context) {
	dictType := c.Param("dictType")
	if dictType == "" {
		result.Wrap(status.BadRequest, "参数错误:dictType").Json(c)
	} else {
		result.Res(service_sys.ApiDictDataByType(dictType)).Json(c)
	}
}

// @Tags  System - 字典管理
// @Summary  获取字典数据详情
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param dictCode path int true "dictCode"
// @Success 200 {object} result.Result "OK"
// @Router /sysDict/data/{dictCode} [get]
func ApiDictDataByCode(c *gin.Context) {
	dictCode, err := strconv.Atoi(c.Param("dictCode"))
	if err != nil {
		result.Wrap(status.BadRequest, "参数读取失败").Json(c)
	} else {
		result.Res(service_sys.GetDictDataById(dictCode)).Json(c)
	}
}

// @Tags  System - 字典管理
// @Summary  添加字典
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param sysDictData body model_sys.SysDictData true "sysDictData"
// @Success 200 {object} result.Result "OK"
// @Router /sysDict/data/add [post]
func ApiAddDictData(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	sysDictData := raw.(model_sys.SysDictDataPo)
	sysDictData.CreateBy = token.GetUk(c)
	result.Res(service_sys.AddDictData(sysDictData)).Json(c)
}

// @Tags  System - 字典管理
// @Summary  修改字典
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param sysDictData body model_sys.SysDictData true "sysDictData"
// @Success 200 {object} result.Result "OK"
// @Router /sysDict/data/edit [put]
func ApiEditDictData(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	sysDictData := raw.(model_sys.SysDictDataPo)
	sysDictData.UpdateBy = token.GetUk(c)
	result.Res(service_sys.EditDictData(sysDictData)).Json(c)
}

// @Tags  System - 字典管理
// @Summary  删除字典[逻辑删除]
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param dictIds query string true "dictIds"
// @Success 200 {object} result.Result "OK"
// @Router /sysDict/data/removes [delete]
func ApiRemoveDictData(c *gin.Context) {
	dictCodes := c.Query("dictCodes")
	result.Res(service_sys.RemoveDictData(dictCodes, token.GetUk(c))).Json(c)
}
